home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MacQForth 1.0 / documentation / Forth Overview < prev    next >
Text File  |  1995-03-21  |  16KB  |  190 lines

  1. tly reducing cycle time.  
  2.    Also, traditional languages are such that the programmer often writes 
  3.    large amounts of code before compiling.  This means an equally long 
  4.    time spent in debugging the code.  Forth operates incrementally, on 
  5.    bits and pieces of code at a time.  The net effect is that it is often 
  6.    claimed that Forth applications can be produced in a fraction of the 
  7.    time it would take to use a "conventional" approach.
  8.    
  9.    
  10.    The Interpreter/Compiler
  11.    ------------------------
  12.    
  13.    Forth contains an interpreter that allows the programmer interactive 
  14.    control over the system.  Definitions entered are immediately compiled 
  15.    into the dictionary and are ready for use as soon as entered.  
  16.    Forth generally uses very short definitions, seldom more than two 
  17.    dozen lines and often much, much shorter.  Short definitions and 
  18.    immediate feedback make debugging and testing quick and easy.  
  19.    Forth's simplicity makes compiling definitions so fast that it doesn't 
  20.    seem that compiling is actually happening.
  21.    
  22.    
  23.    The Dictionary
  24.    --------------
  25.    
  26.    New words are placed in the dictionary as they are encountered and can 
  27.    be made of words that have been previously defined.  For example,
  28.    new words have the following form:
  29.    
  30.    : <name>  <body>  ;
  31.    
  32.    where <name> is the name of the word and <body> is the code that makes 
  33.    up the word.  The word ." takes the characters from the input line, up 
  34.    to an ending " , and places them in the definition so that when executed 
  35.    it will print the characters:
  36.    
  37.    : hi ." Hello" ;  ( define the word, comments in parentheses )
  38.    hi                ( execute the word )
  39.    Hello             ( Forth responds   )
  40.    
  41.    New word definitions can use previously defined words (mechanisms 
  42.    exist in many Forths for forward referencing and recursion):
  43.    
  44.    : hi ( -- ) ." Hello" ;
  45.    : there ( -- ) ." , there!" cr ; ( cr issues a return to next line )
  46.    : hello ( -- ) hi there ;
  47.    hello
  48.      Hello, there!
  49.    
  50.    Note the ( -- ) .  This is called a stack effect comment and good 
  51.    programming dictates that this be used to illustrate what the word 
  52.    expects on the stack and what it leaves after it is done.  So,
  53.    
  54.    : add3 ( a b c -- a+b+c )  + + ;
  55.    
  56.    creates a word that adds the top three stack items.  Note that the 
  57.    stack makes it possible for multiple return items from a word.
  58.    
  59.    Usually, all of the data structures used by a Forth application exist 
  60.    in the dictionary along with the code.  One can create variables, 
  61.    constants, and named dictionary spaces.  Most Forth systems deal only 
  62.    with 16- or 32-bit integer values, though new systems also support 
  63.    floating point numbers.  The words CREATE and ALLOT are used to 
  64.    reserve dictionary space for custom data structures:
  65.    
  66.    CREATE mySpace  1000 ALLOT  ( create mySpace and give it 1000 bytes )
  67.    
  68.    This is similar to the C malloc (though not dynamic) and gives you a 
  69.    chunk of memory with a pointer to the base of the memory.  It is 
  70.    important to note that unlike other languages, giving the name of a 
  71.    variable places the _address_ of the variable on the stack, not the 
  72.    _value_. So,
  73.    
  74.    VARIABLE A    ( create a variable -- really reserves 2 (4) bytes of memory)
  75.    23 A !        ( ! puts the top stack value (23) in the variable )
  76.    A             ( puts the address of A on the stack )
  77.    @             ( fetches the value at the address on the stack, 23 )
  78.    
  79.             
  80.    The Stacks
  81.    ----------
  82.    
  83.    Forth really contains two stacks, one for data and the other for the 
  84.    addresses returned to after a word is called.  The return stack is 
  85.    generally not used but can hold a few values temporarily while a word 
  86.    is executing provided they are removed before the word ends.  The 
  87.    words >R , R@ , and R> will take the top data stack item and place it 
  88.    on the return stack, copy it off the return stack, and pull it from 
  89.    the return stack respectively.
  90.    
  91.    The data stack is where all the action takes place.  Values are put o)
  92.        repeat        ( branch to loop test )
  93.        drop ;        ( drop final return )
  94.    
  95.        
  96.    3. UNTIL
  97.    --------
  98.    
  99.    Standard bottom tested loop.  Code between the BEGIN and UNTIL is 
  100.    executed until a true condition is found,
  101.    
  102.    : echo2           ( echo a line again )        
  103.        begin         ( start the loop    )
  104.          key dup     ( get a key and duplicate the code )
  105.          emit 13 =   ( emit one and see if it was a return )
  106.        until ;       ( go until true )
  107.    
  108.    
  109.    4. AGAIN
  110.    --------
  111.    
  112.    An infinte loop.  Repeat forever until something like LEAVE, QUIT, 
  113.    BYE, or ABORT is executed.
  114.    
  115.    
  116.    5. IF
  117.    -----
  118.    
  119.    Conditional.  Some Forths include variations on CASE statements as well.
  120.    
  121.    : odd?  ( n -- )  \ print "yes" if n is odd, "no" if not
  122.       2 mod 0=       \ is n mod 2 = 0?
  123.       if
  124.         ." no"       \ yes, then n is even
  125.       else
  126.         ." yes"      \ no, then n is odd
  127.       then ;
  128.    
  129.  
  130.    Control structures are only valid within a colon (word) definition.
  131.    
  132.    
  133. V. Data Structures
  134.  
  135.    Most Forths do not have formal data structures, or dynamic memory.  
  136.    They do have VARIABLEs, CONSTANTs, and the ability to reserve 
  137.    dictionary space as indicated above.  This simple arrangement lets the 
  138.    programmer decide how to allocate memory to best suit the task at hand.
  139.    
  140.    VARIABLE A   \ note, some Forths use  23 VARIABLE A to assign an initial
  141.                  \ value to A
  142.                  
  143.    31415 CONSTANT pi   \ no floating point so use scaled integer arithmetic
  144.                         \ instead
  145.                         
  146.    CREATE B    \ define a dictionary entry, does not reseve room
  147.    256 ALLOT    \ reserve 256 bytes from last entry
  148.    
  149.    : { ;   \ create { so we can use it, but do nothing
  150.    : } ( base index -- addr )  \ return the address of an indexed array
  151.       2* + ;  \ 16-bit Forth, use 4* for 32-bit Forth
  152.    
  153.    then we can write:
  154.    
  155.    pi B { 3 } !      \ put 31415 into B(3)
  156.    B { 100 } @   A !  \ get B(100) and put in A
  157.    
  158.    { was used in place of [ as [ and ] are usually used to control the 
  159.    compiler.  Note the spacing between B { 100 } @ and A ! .  It is 
  160.    typical to use more than one space between unrelated words.
  161.  
  162.  
  163. VI.  Conclusion
  164.  
  165.    This was the bearest introduction to what Forth is.  The best way to 
  166.    learn Forth of course is to use Forth.  You'll never learn about it 
  167.    from reading my prose, to be sure!  I strongly recommend getting a 
  168.    hold of some of the sources mentioned in the file MORE FORTH INFO.  
  169.    The _Starting Forth_ book is particularly helpful.  Also, the LESSONs 
  170.    files present the elements of Forth in a step by step exercise format 
  171.    with answers.
  172.    
  173.    Forth has many benefits: speed, simplicity, fast development time, 
  174.    programmer freedom, and high extensibility.  It also has some 
  175.    drawbacks: lack of dynamic memory, lack of local variables, lack of 
  176.    sophisticated data structures.  While many versions correct some of 
  177.    these deficiencies, it is something to consider when deciding if Forth 
  178.    is suited to a particular task.  For example, Forth is not the best 
  179.    choice for a computer algebra system, though it will work.  (Ask me, I 
  180.    tried it :)
  181.    
  182.    QForth is a simple, simple Forth.  I chose it because of this 
  183.    simplicity, but do not confuse QForth with full powered Forths.  They 
  184.    are completely filled out and include abilities to extend the compiler 
  185.    itself, something QForth lacks.  Mops and Yerk for the Macintosh are 
  186.    examples of Forths that are complete and integrated fully with their 
  187.    environment.
  188.    
  189.    Now, we learn by doing, so do Forth and learn!
  190.